package com.flyduck.web;

import com.flyduck.domain.SysJob;
import com.flyduck.service.SysJobService;
import com.flyduck.vo.AjaxResult;
import com.flyduck.vo.PageResult;
import com.flyduck.vo.QueryVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: flyduck-admin
 * @description: 岗位管理
 * @author: flyduck
 * @create: 2020-05-06 14:01
 **/
@Controller
@RequestMapping("/job")
public class SysJobController {

    @Autowired
    private SysJobService sysJobService;

    /**
     * 首页
     * @return
     */
    @RequiresPermissions("job:list")
    @GetMapping
    public String index(){
        return "job";
    }

    /**
     * 分页数据
     * @return
     */
    @RequiresPermissions("job:list")
    @PostMapping("/list")
    @ResponseBody
    public PageResult findPage(QueryVo queryVo){
        return sysJobService.findPage(queryVo);
    }

    /**
     * 保存岗位
     * @param sysJob
     * @return
     */
    @RequiresPermissions("job:save")
    @GetMapping("save")
    @ResponseBody
    public AjaxResult save(SysJob sysJob){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysJobService.save(sysJob);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("保存失败");
        }
        return ajaxResult;
    }

    /**
     * 更新岗位
     * @param sysJob
     * @return
     */
    @RequiresPermissions("job:update")
    @GetMapping("update")
    @ResponseBody
    public AjaxResult update(SysJob sysJob){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysJobService.update(sysJob);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("更新失败");
        }
        return ajaxResult;
    }

    /**
     * 根据id查询岗位
     * @param id
     * @return
     */
    @GetMapping("getJobById")
    @ResponseBody
    public SysJob getJobById(Long id){
        return sysJobService.getJobById(id);
    }


    /**
     * 返回所有的职位
     * @return
     */
    @GetMapping("/all")
    @ResponseBody
    public List<SysJob> getAll(){
        return sysJobService.findList();
    }

    /**
     * 启用/禁用岗位
     * @return
     */
    @RequiresPermissions(value = {"job:enable","job:disable"},logical = Logical.OR)
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(Long id,Boolean enabled){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysJobService.updateStatus(id,enabled);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("更新失败");
        }
        return ajaxResult;
    }
}
